package copy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import copy.com.CustomException;
import copy.dao.CategoryDao;
import copy.pojo.Category;
import copy.pojo.Dish;
import copy.pojo.Setmeal;
import copy.service.CategoryService;
import copy.service.DishService;
import copy.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author 小思
 * @Description //TODO
 * @Date $ $
 * @Param $
 * @return $
 **/

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    public void removeWithDishSetmeal(Long ids) {
        //category id 关联的两表若有 则为关联数据 不能直接删除主表关联数据
        LambdaQueryWrapper<Dish> dishla = new LambdaQueryWrapper<>();
        dishla.eq(Dish::getCategoryId, ids);
        int dishcount = dishService.count(dishla);
        if (dishcount > 0) {
            throw new CustomException("关联数据，先解除关联关系才能删除");

        }
        LambdaQueryWrapper<Setmeal> Setmealla = new LambdaQueryWrapper<>();
        Setmealla.eq(Setmeal::getCategoryId, ids);
        int Setcounts = setmealService.count(Setmealla);
        if (Setcounts > 0) {
            throw new CustomException("关联数据，先解除关联关系才能删除");

        }
        //不是关联数据
        this.removeById(ids);

    }
}
